package com.project.weimi.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.project.weimi.R;
import com.project.weimi.application.Constant;
import com.project.weimi.entity.response.GetRegisterCodeBean;
import com.project.weimi.entity.response.RegisterSuccessBean;
import com.project.weimi.interfaces.MyCallBack;
import com.project.weimi.utils.CheckUtils;
import com.project.weimi.utils.GsonUtil;
import com.project.weimi.utils.NetUtils;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.utils.TimeCount;
import com.project.weimi.utils.XUtil;
import com.project.weimi.widget.ToastMessage;
import com.project.weimi.widget.UnderLineEditText;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class RegisterActivity extends BaseActivity{

	private UnderLineEditText et_register_phone,et_verificationCode,et_register_pwd;
	private Button btn_confirm;
	private Button tv_send_verificationCode;
	// 获得的验证码
	private String responseCode;
	TimeCount timeCount;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_register, "注册");
		
		initView();
		
		
	}
	
	private void initView(){
		et_register_phone = (UnderLineEditText)findViewById(R.id.et_register_phone);
		et_verificationCode = (UnderLineEditText)findViewById(R.id.et_verificationCode);
		et_register_pwd = (UnderLineEditText)findViewById(R.id.et_register_pwd);
		tv_send_verificationCode = (Button)findViewById(R.id.tv_send_verificationCode);
		btn_confirm = (Button)findViewById(R.id.btn_confirm);
		
		timeCount = new TimeCount(60*1000, 1000, tv_send_verificationCode);
		//监听
		SetClickListener.setClickListener(listener, btn_confirm,tv_send_verificationCode);
		
	}
	
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.btn_confirm: // 发送注册请求
				
				if(!NetUtils.isConnected(RegisterActivity.this)){
					ToastMessage.showToast(RegisterActivity.this, "网络不给力，请检查网络设置");
				} else {
					initRegister();
				}
				
				break;
			case R.id.tv_send_verificationCode: // 获取手机验证码
				
				if(!NetUtils.isConnected(RegisterActivity.this)){
					ToastMessage.showToast(RegisterActivity.this, "网络不给力，请检查网络设置");
					return;
				}
				
				String phone = et_register_phone.getText().toString().replace(" ", "").trim();
				if(TextUtils.isEmpty(phone)){
					ToastMessage.showMyToast(RegisterActivity.this, "请输入手机号码！");
					return;
				}
				if(!phone.matches(CheckUtils.phoneNum)){
					ToastMessage.showMyToast(RegisterActivity.this, "请输入正确手机号码！");
					return;
				}
				timeCount.start();
				
				sendAuthCode(phone);
				
				break;
			}
		}
	};
	
	// 发送注册请求
	private void initRegister(){
		String phone = et_register_phone.getText().toString().replace(" ", "").trim();
		String code = et_verificationCode.getText().toString().replace(" ", "").trim();
		String pwd = et_register_pwd.getText().toString().replace(" ", "").trim();
		
		if(TextUtils.isEmpty(phone)){
			ToastMessage.showMyToast(RegisterActivity.this, "请输入手机号码！");
			return;
		}
		if(!phone.matches(CheckUtils.phoneNum)){
			ToastMessage.showMyToast(RegisterActivity.this, "请输入正确手机号码！");
			return;
		}
		if(TextUtils.isEmpty(code)){
			ToastMessage.showMyToast(RegisterActivity.this, "请输入验证码！");
			return;
		}
		if(!code.equals(responseCode)){
			ToastMessage.showMyToast(RegisterActivity.this, "您输入的验证码有误！");
			return;
		}
		if(TextUtils.isEmpty(pwd)){
			ToastMessage.showMyToast(RegisterActivity.this, "请输入密码！");
			return;
		}
		if(pwd.length() < 6){
			ToastMessage.showMyToast(RegisterActivity.this, "密码长度在6-18位之间");
			return;
		}
		if(!pwd.matches(CheckUtils.pwdPattern)){
			ToastMessage.showMyToast(RegisterActivity.this, "必须字母开头，可以为纯字母，也可以为数字、字母组合");
			return;
		}
		
		// 发送注册请求
		sendRegisterRequestHttp(phone,pwd);
		
	}
	
	private void sendRegisterRequestHttp(String memberId,String passWord){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		map.put("passWord", passWord);
		
		XUtil.post(Constant.REGISTER, map, new MyCallBack<String>(){
			
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(null != result && !"".equals(result)){
					//Log.e("TAG", "注册返回==="+result);
					
					try {
						JSONObject obj = new JSONObject(result);
						boolean a = !TextUtils.isEmpty(obj.getString("result"));
						boolean b = TextUtils.equals(obj.getString("result"), "0");
						if(a && b){
							ToastMessage.showMyToast(RegisterActivity.this, "该手机号已经注册");
						}else if(obj.getJSONObject("result").has("memberId")){
							RegisterSuccessBean bean = (RegisterSuccessBean) GsonUtil.jsonToBean(
									obj.getString("result"), RegisterSuccessBean.class);
							Log.e("TAG", "++++" + bean.toString());
							ToastMessage.showMyToast(RegisterActivity.this, "注册成功！");
							// 跳到登录页面
							Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
							startActivity(intent);
							RegisterActivity.this.finish();
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
			
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
			}
		});
	}
	
	// 发送验证码请求
	private void sendAuthCode(String memberId){
		
		
		
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		
		XUtil.post(Constant.REGiSTER_AUTH_CODE, map, new MyCallBack<String>(){
			
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(null != result && !"".equals(result)){
					
					GetRegisterCodeBean bean = (GetRegisterCodeBean) GsonUtil.jsonToBean(result, GetRegisterCodeBean.class);
					responseCode = bean.getResult();
					
					Log.e("TAG", "验证码==="+responseCode);
					if(!"1".equals(responseCode)){
						ToastMessage.showMyToast(RegisterActivity.this, "验证码已发送到您手机");
					} else {
						ToastMessage.showMyToast(RegisterActivity.this, "该手机号已经注册");
					}
					
				}
			}
			
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
			}
		});
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}
	
	
}
